package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.CustomEditText;
import com.hengxinguotong.zhihuichengjian.widget.font.HXEditTextView;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.lidroid.xutils.http.RequestParams;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/28.
 */

public class FeedBackActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.tv_title)
    HXTextView tvTitle;
    @BindView(R.id.tv_confirm)
    HXTextView tvConfirm;
    @BindView(R.id.et_content)
    CustomEditText etContent;
    @BindView(R.id.tv_size)
    HXTextView tvSize;

    private FeedBackActivity mActivity;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        mActivity = this;

        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                content = editable.toString().trim();
                if(content.length()>0){
                    tvSize.setText(content.length() + "/256");
                }

            }
        });
    }


    @OnClick({R.id.iv_back, R.id.tv_confirm})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_confirm:
                feedBack();
                break;
        }
    }

    private void feedBack(){
        if(null == content || content.length()<=0){
            showToast("请输入您的意见或者建议");
        }else{
            RequestParams params = new RequestParams();
            params.addBodyParameter("description", content);
            Utils.requestData("", mActivity, "/userFeedback/save/" + SPUtil.getString(mActivity, "uuid"), params, new Utils.ListenerJson() {
                @Override
                public void onSuccess(String json) {
                    if(!Utils.isEmpty(json)){
                        try {
                            JSONObject object = new JSONObject(json);
                            int status = object.getInt("status");
                            String msg = object.getString("msg");
                            if(1 == status){
                                showToast(msg);
                                finish();
                            }else{
                                showToast(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }

                @Override
                public void onFailure(String json) {
                    showToast("提交失败，请稍后重试");
                }
            });
        }

    }

}
